#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.security.oauth2.config;

import ${package}.common.bean.vo.JsonResult;
import ${package}.common.emum.ReturnCodeEnum;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description: token的异常类
 *
 * @author ywh
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
@Component
public class YkAuthExceptionEntryPoint implements AuthenticationEntryPoint {

    /**
     * Description:
     * <资源服务器认证失败后处理逻辑>
     * @author ywh
     * @date 14:31 2019/8/8
     * @param httpServletRequest 1
     * @param httpServletResponse 2
     * @param authException 3
     **/
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException authException) {

        // http code返回401未认证
	    httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        httpServletResponse.setHeader("Content-Type", "application/json;charset=UTF-8");
        try {
            log.error(ReturnCodeEnum.TOKEN_INVALID.getMsg());
            httpServletResponse.getWriter().write(JSON.toJSONString(JsonResult.getInstant(ReturnCodeEnum.TOKEN_INVALID)));
            httpServletResponse.getWriter().flush();
            httpServletResponse.getWriter().close();
        } catch (IOException e) {
            log.error("response error", e);
        }

    }
}
